﻿Option Explicit On
Imports System.IO
Imports System.Net
''' <summary>
''' Clase que obtiene datos del piloto del archivo de VATSIM de pilotos online
''' Devuelve getDatos Array con los 48 campos de datos
''' </summary>
''' <remarks></remarks>
Public Class IvaoDatosPiloto
    Private datos(48) As String

    Public Sub New()
        ConectaIvao()
    End Sub
    Public Sub New(ByVal codigo As String)
        cnfIVAO = codigo
        ConectaIvao()
    End Sub
    ''' <summary>
    ''' Contructor copia de la clase
    ''' </summary>
    ''' <param name="copia"></param>
    ''' <remarks></remarks>
    Public Sub New(ByRef copia As IvaoDatosPiloto)
        Me.datos = copia.getDatos
    End Sub

    Public ReadOnly Property getDatos() As String()
        Get
            Return Me.datos
        End Get
    End Property

    Private Sub ConectaIvao()
        Dim URLString As String = "http://www.ivao.aero/whazzup/status.txt"

        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        Dim line As String
        Dim campos() As String
        Dim campo As String
        Dim url As ArrayList = New ArrayList()

        Try
            webRequest = webRequest.Create(URLString)
            webresponse = webRequest.GetResponse()
            inStream = New StreamReader(webresponse.GetResponseStream())
            While inStream.EndOfStream = False
                line = inStream.ReadLine()


                If line.Substring(0, 5) = "url0=" Then
                    url.Add(line.Substring(5).ToString)
                End If


            End While
            If url.Count = 0 Then Return
            URLString = url(cuentaUrls).ToString
            'Debug.WriteLine(URLString)
            cuentaUrls = cuentaUrls + 1
            If cuentaUrls = url.Count Then cuentaUrls = 0

            inStream = Nothing
            'Randomize()
            'URLString = url(CInt(Int((url.Count - 1) * Rnd() + 0.5))).ToString
            webRequest = webRequest.Create(URLString)
            webresponse = webRequest.GetResponse()
            inStream = New StreamReader(webresponse.GetResponseStream())

            While inStream.EndOfStream = False
                line = inStream.ReadLine()
                'Debug.Write(inStream.ReadToEnd())
                'Debug.Write(line)

                If line.Substring(0) = "!CLIENTS" Then
                    Exit While
                End If
            End While

            '0 callsign:  1 cid: 2 realname: 3 clienttype: 4 frequency:
            '5 latitude:  6 longitude: 7 altitude: 8 groundSpeed: 9 planned_aircraft: 10 planned_tascruise:
            '11 planned_depairport: 12 planned_altitude: 13 planned_destairport: 14 server: 15 protrevision:
            '16 rating: 17 transponder: 18 facilitytype: 19 visualrange: 20 planned_revision: 21 planned_flighttype: 
            '22 planned_deptime: 23 planned_actdeptime: 24 planned_hrsenroute: 25 planned_minenroute: 
            '26 planned_hrsfuel: 27 planned_minfuel: 28 planned_altairport: 29 planned_remarks: 30 planned_route:
            '31 planned_depairport_lat: 32 planned_depairport_lon: 33 planned_destairport_lat:
            '34 planned_destairport_lon: 35 atis_message: 36 time_last_atis_received: 37 time_logon: 38 heading:
            '39 QNH_iHg: 40 QNH_Mb
            campos = inStream.ReadLine().Split(New Char() {":"c})


            Do
                campo = campos(1)
                If campo = cnfIVAO Then
                    Exit Do
                End If
                campos = inStream.ReadLine().Split(New Char() {":"c})
            Loop While campos.Length = 49
            If campos.Length = 49 Then
                For i = 0 To 48
                    Me.datos(i) = campos(i)
                Next

            Else
                Me.datos(0) = Nothing
                Me.datos(1) = Nothing
            End If
            inStream.Close()
            '***********************
            '***********************

        Catch ex As WebException
            'MessageBox.Show("Ha habido un problema al conectar con la URL de VATSIM", "Error de conexión VATSIM", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
            Me.datos(0) = Nothing : Me.datos(1) = Nothing



        End Try
    End Sub
End Class
